package com.shuqi.activity;

import android.app.Dialog;
import android.app.ProgressDialog;
import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.view.KeyEvent;
import android.widget.Toast;

import com.shuqi.application.ApplicationCrashHandler;
import com.shuqi.common.Config;
import com.shuqi.common.ConfigVersion;
import com.shuqi.common.utils.Log4an;
import com.shuqi.model.manager.MyAccountManager;
import com.shuqi.statistics.StatisticsUtils;

/**
 * 所有Activity的父类
 */
public abstract class FragmentActivityBase extends FragmentActivity {

    public static final int PROGRESS_DIALOG = 0;
    private Toast toast;

    protected Runnable r;
    protected Thread t;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        ApplicationCrashHandler.showed(this);
        if (savedInstanceState != null || ConfigVersion.hasNull()) {
            ConfigVersion.setTelInfos(this, Config.ROOT_PATH, Config.VERSION_INFO, Config.SOFT_ID,
                    Config.MIN_SDK_VERSION, 0, MyAccountManager.getInstance(this).getUserId(),
                    MyAccountManager.getInstance(this).getSession());
        }
        Log4an.init(this, Config.ROOT_PATH);
    }

    @Override
    protected Dialog onCreateDialog(int id) {
        switch (id) {
            case 0:// 显示等待dialog
                ProgressDialog dialog = null;
                if (getParent() != null) {
                    dialog = new ProgressDialog(getParent());
                } else {
                    dialog = new ProgressDialog(this);
                }
                // dialog.setMessage(getResources().getString(R.string.hint_waiting));
                // dialog.setMessage(this.getString(R.string.hint_waiting));
                return dialog;
        }
        return super.onCreateDialog(id);
    }

    /** 显示消息 */
    public void showMsg(final String msg) {
        Runnable r = new Runnable() {
            @Override
            public void run() {
                try {
                    if (toast == null) {
                        toast = Toast.makeText(FragmentActivityBase.this, msg, Toast.LENGTH_SHORT);
                    } else {
                        toast.setText(msg);
                    }
                    if (!isFinishing()) {
                        toast.show();
                    }
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        };
        runOnUiThread(r);
    }

//    /** 显示消息 */
//    public void showMsg(final String msg, boolean isLong) {
//        Runnable r = new Runnable() {
//            @Override
//            public void run() {
//                try {
//                    if (toast == null) {
//                        toast = Toast.makeText(FragmentActivityBase.this, msg, Toast.LENGTH_LONG);
//                    } else {
//                        toast.setText(msg);
//                    }
//                    toast.show();
//                } catch (Exception e) {
//                    e.printStackTrace();
//                }
//            }
//        };
//        runOnUiThread(r);
//    }

    @Override
    protected void onStop() {
        super.onStop();
    }

    @Override
    protected void onResume() {
        ApplicationCrashHandler.showed(this);
        StatisticsUtils.onResume(this);
        super.onResume();
    }

    @Override
    protected void onPause() {
        super.onPause();
        StatisticsUtils.onPause(this);
        ApplicationCrashHandler.clear();
    }
    
    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if(keyCode == KeyEvent.KEYCODE_MENU && event.getRepeatCount() > 0){          
            event.startTracking();
            return true;
        }
        return super.onKeyDown(keyCode, event);
    }

    @Override
    public boolean onKeyLongPress(int keyCode, KeyEvent event) {
        System.out.println("onkeylongpress");
        if(keyCode==KeyEvent.KEYCODE_MENU){
            openOptionsMenu();
        }
        return super.onKeyLongPress(keyCode, event);
    }
}
